package sample.tangocho.activity;

import sample.tangocho.model.Tangocho;
import sample.tangocho.service.CreateTangochoService;
import sample.tangocho.service.CreateTangochoServiceImpl;
import sample.tangocho.service.EditTangochoService;
import sample.tangocho.service.EditTangochoServiceImpl;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class EditTangochoActivity extends Activity {
	
	private CreateTangochoService createTangochoService;
	private EditTangochoService editTangochoService;
	
	private int tangochoId;
	

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_tangocho);

		// TODO DI
		createTangochoService = new CreateTangochoServiceImpl(
				getApplicationContext());
		editTangochoService = new EditTangochoServiceImpl(getApplicationContext());
        
        Intent intent = getIntent();
        tangochoId = intent.getIntExtra("tangochoId", -1);
        
        if (tangochoId != -1) {
    		EditText titleText = (EditText) findViewById(R.id.edit_title);
    		titleText.setText(intent.getStringExtra("tangochoTitle"));
        }
    }
    

	public void createOrEditTangocho(View view) {

		EditText editTitle = (EditText) findViewById(R.id.edit_title);
		
		String title = editTitle.getText().toString();

		if (title.length() > 0) {
			if (tangochoId == -1) {
			createTangochoService
					.createTangocho(title);
			} else {
				Tangocho tangocho = new Tangocho();
				tangocho.setId(tangochoId);
				tangocho.setTitle(title);
				editTangochoService.editTangocho(tangocho);
			}
			finish();
		}
	}

}
